﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：查看模型计算结果
 */

var keyValue = request('modelId');
var defaultCompanyId = request('companyId');
var defaultKjnd = request('defaultKjnd');
var warnId = request('warnId');


var userdefault = {};
var selectedRow;
var step2List = [];

var bootstrap = function ($, learun) {
    "use strict";

    var jfGrid1 = null;
    var companyId = '';

    // 保存数据
    var acceptClick = function (type) {// 0保存并新增 1保存

    };

    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            if(!!defaultCompanyId) {
                userdefault.companyId = defaultCompanyId;
            }
            page.bind();
            $("#kjnd").val(defaultKjnd);
            page.initData();
        },
        bind: function () {
            // 优化滚动条
            $('.lr-layout-wrap').lrscroll();
            $('#multiple_condition_query').lrMultipleQuery(function (queryJson) {
                // 调用后台查询
                // queryJson 查询条件
                page.search(queryJson);

            }, 200, 888);


            // $('#month').lrselect({
            //     type: 'default',
            //     // 展开最大高度
            //     maxHeight: 300,
            //     placeholder: '月份',
            //     data: [
            //         {'id': '01', 'text': '01'},
            //         {'id': '02', 'text': '02'},
            //         {'id': '03', 'text': '03'},
            //         {'id': '04', 'text': '04'},
            //         {'id': '05', 'text': '05'},
            //         {'id': '06', 'text': '06'},
            //         {'id': '07', 'text': '07'},
            //         {'id': '08', 'text': '08'},
            //         {'id': '09', 'text': '09'},
            //         {'id': '10', 'text': '10'},
            //         {'id': '11', 'text': '11'},
            //         {'id': '12', 'text': '12'}
            //     ]
            // });
            // 公司选择
            $('#companyId').lrselect({
                type: 'tree',
                placeholder: '==全部单位==',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {
                    if (!item || item.value == '-1') {
                        companyId = '';
                    } else {
                        companyId = item.value;
                        $('#departmentNames').lrselectRefresh({
                            // 访问数据接口地址
                            url: config.base_server() + 'departments/tree?access_token=' + config.getToken().access_token,
                            // 访问数据接口参数
                            param: {companyId: companyId, parentId: '0'},
                        });
                    }
                }
            });
            $('#companyId').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);

            // 部门选择
            $('#departmentNames').lrselect({
                type: 'multiple',
                placeholder: '请选择部门',
                // 展开最大高度
                maxHeight: 300,
                value: "text",
                text: "text",
                title: "text",
                // 是否允许搜索
                allowSearch: true
            });

            // 整改
            $('#btn_deal').on('click', function () {
                learun.layerForm({
                    id: 'warn_deal' + keyValue,
                    title: '告警整改',
                    url: config.webportal_server + '/pages/workflow/wfmyTask/custmerWorkFlowForm.html?keyValue=' + keyValue + '&modelId=' + keyValue + '&warnId=' + warnId + '&shcemeCode=warn_deal&tabIframeId=5456a5f791844dbaaa1474af2a70e8db&type=0',
                    width: 1000,
                    height: 800,
                    maxmin: true,
                    btn: null
                });
            });
            // 忽略
            $('#btn_ingore').on('click', function () {
                learun.layerForm({
                    id: 'warn_ingore' + keyValue,
                    title: '告警忽略',
                    url: config.webportal_server + 'pages/finance/audit_warn/ignore/form.html?warnId=' + warnId + '&access_token=' + config.getToken().access_token,
                    width: 450,
                    height: 280,
                    callBack: function (id) {
                        return top[id].acceptClick();
                    }
                });
            });

            //下载
            $('#btn_download').on('click', function () {
               var companyId = $("#companyId").lrselectGet();
               var kjnd = $("#kjnd").val();
               location.href = config.base_server() + 'auditmodels/exportExcel?modelId=' + keyValue + '&companyId=' + companyId + '&kjnd=' + kjnd + '&warnId=' + warnId + '&access_token=' + config.getToken().access_token;
            });

            // 凭证数据
            jfGrid1 = $('#usecarGrid').jfGrid({
                headData: [
                    {label: "单位", name: "nameorganization", width: 160, align: "left"},
                    {label: "年度", name: "yearly", width: 60, align: "center"},
                    {label: "部门", name: "namedepartment", width: 100, align: "left"},
                    {label: "报销人", name: "paymentperson", width: 100, align: "left"},
                    {label: "驾驶员", name: "driver", width: 100, align: "left"},
                    {label: "报销事项", name: "content", width: 250, align: "left"},
                    {
                        label: '金额', name: 'amount10', width: 110, align: 'right',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: "出差人", name: "person", width: 180, align: "left"},
                    {label: "车牌号", name: "cph", width: 100, align: "left"},
                    {label: "报销时间", name: "createdate", width: 140, align: "center"},
//                    {label: "交通工具", name: "travelmode", width: 90, align: "center"},
//                    {label: "出发地", name: "startcity", width: 140, align: "left"},
                    {label: "目的地", name: "stopcity", width: 140, align: "left"},
                    {label: "出发时间", name: "startdate", width: 140, align: "center"},
                    {label: "交车时间", name: "leavedate", width: 140, align: "center"},
                    {label: "单位id", name: "companyId", width: 1, align: "center", hidden: true},
                ],
                height: 580
            });
        },
        initData: function () {
            if (!!keyValue) {
                page.search();
            }
            if (!!warnId) {
                $.ajax({
                    url: config.base_server() + 'auditmodelwarns/updateReadFlag?id=' + warnId + '&access_token=' + config.getToken().access_token,
                    type: "GET",
                    success: function (data) {
                    }
                });
            }
        },
        search: function (param) {
            $('#lr_form_bg').fadeIn();
            param = param || {};

            var companyId = $("#companyId").lrselectGet();
            if(!companyId && !!defaultCompanyId) {
                companyId = defaultCompanyId;
            }
            var departmentNames = $("#departmentNames").lrselectGet();
            var kjnd = $("#kjnd").val();
            // var month = $("#month").lrselectGet();

            param.companyId = companyId;
            param.departmentNames = departmentNames;
            param.kjnd = kjnd;
            // param.month = month;

            page.refreshData(param);
        },
        refreshData: function (param) {
            learun.httpAsync('GET', config.base_server() + 'auditmodels/previewResult?modelId=' + keyValue  + '&warnId=' + warnId + '&access_token=' + config.getToken().access_token, param, function (data) {
                $('#lr_form_bg').fadeOut();
                $('.lr-layout-wrap').lrSetFormData(data);
                $("#modelName").html(data.modelName);
                $('#usecarGrid').jfGridSet('refreshdata', data.step1List);
                step2List = data.step1List;
                $('#lr_form_bg').fadeOut();
            });
        }
    };

    page.init();
}
